package org.jinxing.service.impl;

import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.jinxing.dao.UsersInfoDAO;
import org.jinxing.model.UsersInfo;
import org.jinxing.model.UsersInfoExample;
import org.jinxing.model.UsersInfoExample.Criteria;
import org.jinxing.service.UsersInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service("usersInfoService")
public class UsersInfoServiceImpl implements UsersInfoService {
	protected static String className = UsersInfoServiceImpl.class.getName();
	protected static Log logger = LogFactory.getLog(className);//日志对象
	
	@Autowired
	UsersInfoDAO usersInfoDao;
	
	@Override
	public UsersInfo getUsersInfo(String lgName, String passwd) {
		UsersInfo result = null;
		UsersInfoExample example = new UsersInfoExample();
		Criteria criteria = example.createCriteria();
		criteria.andLgnameEqualTo(lgName);
		criteria.andPasswdEqualTo(passwd);
		List<UsersInfo> infoList = usersInfoDao.selectByExample(example);
		if(null != infoList && 1 == infoList.size()){
			result = infoList.get(0);
		}
		return result;
	}

	@Override
	public List<UsersInfo> getAllUsersInfo(Integer roleId, Integer pageNo,
			Integer pageSize) {
		UsersInfoExample example = new UsersInfoExample();
		Criteria criteria = example.createCriteria();
		criteria.andRoleidGreaterThan(roleId);
		if(pageSize > 0){
			example.setLimitClauseStart((pageNo-1)*pageSize);
			example.setLimitClauseCount(pageSize);
		}
		example.setOrderByClause(" id desc,roleId asc");
		List<UsersInfo> resultList = this.usersInfoDao.selectByExample(example);
		logger.info("<|>"+className+"<|>getAllUsersInfo<|>{"+roleId+","+pageNo+","+pageSize+"}<|>"+resultList.size()+"<|>");
		return resultList;
	}

	@Override
	public Integer countAllUsersInfo(Integer roleId) {
		UsersInfoExample example = new UsersInfoExample();
		Criteria criteria = example.createCriteria();
		criteria.andRoleidGreaterThan(roleId);
		Integer count = this.usersInfoDao.selectCountByExample(example);
		logger.info("<|>"+className+"<|>countAllUsersInfo<|>"+roleId+"<|>"+count+"<|>");
		return count;
	}

	@Override
	public boolean saveOrUpdate(UsersInfo userInfo) {
		boolean result = false;
		int num = 0;
		if(null != userInfo.getId()){
			num = this.usersInfoDao.updateByPrimaryKeySelective(userInfo);
			if(1 == num){
				result = true;
				logger.info("<|>"+className+"<|>saveOrUpdate<|>"+userInfo+"<|>更新ok<|>");
			}else{
				logger.info("<|>"+className+"<|>saveOrUpdate<|>"+userInfo+"<|>failure:"+num+"<|>");
			}
		}else{
			num = this.usersInfoDao.insert(userInfo);
			if(0 < num){
				result = true;
				logger.info("<|>"+className+"<|>saveOrUpdate<|>"+userInfo+"<|>更新ok<|>");
			}else{
				logger.info("<|>"+className+"<|>saveOrUpdate<|>"+userInfo+"<|>failure:"+num+"<|>");
			}
		}
		
		return result;
	}

	@Override
	public UsersInfo getById(Integer id) {
		return this.usersInfoDao.selectByPrimaryKey(id);
	}

	@Override
	public UsersInfo getByLgName(String lgName) {
		UsersInfoExample example = new UsersInfoExample();
		Criteria criteria = example.createCriteria();
		criteria.andLgnameEqualTo(lgName);
		List<UsersInfo> list = this.usersInfoDao.selectByExample(example);
		UsersInfo result = null;
		if(null != list){
			if (list.size() == 1) {
				result = list.get(0);
			}else if(list.size() > 1){
				logger.warn("<|>"+className+"<|>saveOrUpdate<|>"+lgName+"<|>"+list.size()+"<|>");
			}
		}
		return result;
	}

}
